package cn.stylefeng.guns.modular.demos.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.demos.entity.EduAttendanceInfo;
import cn.stylefeng.guns.modular.demos.entity.EduCourseSelectionInfo;
import cn.stylefeng.guns.modular.demos.model.params.EduCourseSelectionInfoParam;
import cn.stylefeng.guns.modular.demos.model.result.BarChartRsp;
import cn.stylefeng.guns.modular.demos.service.EduAttendanceInfoService;
import cn.stylefeng.guns.modular.demos.service.EduCourseSelectionInfoService;
import cn.stylefeng.guns.sys.core.auth.LoginContextSpringSecutiryImpl;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.time.LocalDateTime;


/**
 * 选课信息表控制器
 *
 * @author Frey Wang
 * @Date 2022-11-09 10:33:46
 */
@Controller
@RequestMapping("/eduCourseSelectionInfo")
public class EduCourseSelectionInfoController extends BaseController {

    private String PREFIX = "/demos/eduCourseSelectionInfo";

    @Autowired
    private EduCourseSelectionInfoService eduCourseSelectionInfoService;

    @Autowired
    private EduAttendanceInfoService eduAttendanceInfoService;

    @Autowired
    private LoginContextSpringSecutiryImpl loginContextSpringSecutiry;

    /**
     * 跳转到主页面
     *
     * @author Frey Wang
     * @Date 2022-11-09
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/eduCourseSelectionInfo.html";
    }

    /**
     * 新增页面
     *
     * @author Frey Wang
     * @Date 2022-11-09
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/eduCourseSelectionInfo_add.html";
    }

    /**
     * 编辑页面
     *
     * @author Frey Wang
     * @Date 2022-11-09
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/eduCourseSelectionInfo_edit.html";
    }

    /**
     * 我的签到
     * @return
     */
    @RequestMapping("/my/sign")
    public String sign(){
        return PREFIX + "/eduCourseSelectionInfo_sign.html";
    }

    /**
     * 签到管理
     * @return
     */
    @RequestMapping("/sign/manager")
    public String signManager(){
        return PREFIX + "/eduCourseSelectionInfoSign_manager.html";
    }

    /**
     * 数据分析
     * @return
     */
    @RequestMapping("/echart")
    public String echart(){
        return PREFIX + "/eduCourseSelectionInfoEchart.html";
    }

    /**
     * 新增接口
     *
     * @author Frey Wang
     * @Date 2022-11-09
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(EduCourseSelectionInfoParam eduCourseSelectionInfoParam) {
        eduCourseSelectionInfoParam.setSignTime(LocalDateTime.now());
        this.eduCourseSelectionInfoService.add(eduCourseSelectionInfoParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author Frey Wang
     * @Date 2022-11-09
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(EduCourseSelectionInfoParam eduCourseSelectionInfoParam) {
        this.eduCourseSelectionInfoService.update(eduCourseSelectionInfoParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author Frey Wang
     * @Date 2022-11-09
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(EduCourseSelectionInfoParam eduCourseSelectionInfoParam) {
        this.eduCourseSelectionInfoService.delete(eduCourseSelectionInfoParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author Frey Wang
     * @Date 2022-11-09
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(EduCourseSelectionInfoParam eduCourseSelectionInfoParam) {
        EduCourseSelectionInfo detail = this.eduCourseSelectionInfoService.getById(eduCourseSelectionInfoParam.getUniqueKey());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author Frey Wang
     * @Date 2022-11-09
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(EduCourseSelectionInfoParam eduCourseSelectionInfoParam) {
        return this.eduCourseSelectionInfoService.findPageBySpec(eduCourseSelectionInfoParam);
    }

    /**
     * 我的签到记录
     * @return
     */
    @ResponseBody
    @RequestMapping("mysign")
    public LayuiPageInfo mySign(EduCourseSelectionInfoParam eduCourseSelectionInfoParam){
        String studentId = loginContextSpringSecutiry.getUser().getAccount();
        eduCourseSelectionInfoParam.setStudentId(studentId);
        return this.eduAttendanceInfoService.findPageBySpec(eduCourseSelectionInfoParam);
    }

    /**
     * 签到记录
     * @return
     */
    @ResponseBody
    @RequestMapping("sign/list")
    public LayuiPageInfo signList(EduCourseSelectionInfoParam eduCourseSelectionInfoParam){
        String studentId = loginContextSpringSecutiry.getUser().getAccount();
        eduCourseSelectionInfoParam.setStudentId(studentId);
        return this.eduCourseSelectionInfoService.findSignPageBySpec(eduCourseSelectionInfoParam);
    }

    @ResponseBody
    @RequestMapping("sign/edit")
    public ResponseData signEdit(EduCourseSelectionInfoParam eduCourseSelectionInfoParam){
        this.eduCourseSelectionInfoService.update(eduCourseSelectionInfoParam);
        EduCourseSelectionInfo eduCourseSelectionInfo = eduCourseSelectionInfoService.getById(eduCourseSelectionInfoParam.getUniqueKey());
        if(eduCourseSelectionInfoParam.getStatus().intValue()==4){
                // to do
            eduAttendanceInfoService.deleteByRefKeyAndStuendId(eduCourseSelectionInfo.getUniqueKey(),eduCourseSelectionInfo.getStudentId());
        }else if(eduCourseSelectionInfoParam.getStatus().intValue()==1){
            EduAttendanceInfo eduAttendanceInfo = new EduAttendanceInfo();
            BeanUtils.copyProperties(eduCourseSelectionInfo,eduAttendanceInfo);
            eduAttendanceInfo.setRefKey(eduCourseSelectionInfo.getUniqueKey());
            eduAttendanceInfo.setUniqueKey("");
            eduAttendanceInfo.setSignTime(LocalDateTime.now());
            eduAttendanceInfoService.save(eduAttendanceInfo);
        }
        return ResponseData.success();
    }

    @ResponseBody
    @RequestMapping("sign/reset")
    public ResponseData signReset(@RequestParam("course") String courseName){
         class CourseDao {
            private String jdbcURL = "jdbc:mysql://8.130.36.2:3306/guns_advanced";
            private String jdbcUsername = "user";
            private String jdbcPassword = "123456";
            public void resetCourseStatus(String courseName) {
                System.out.println(courseName);
                String sql = "UPDATE guns_advanced.edu_course_selection_info SET status = 2 WHERE curriculum_name = ?";
                try (Connection connection = DriverManager.getConnection(jdbcURL, jdbcUsername, jdbcPassword);
                     PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
                    preparedStatement.setString(1, courseName);
                    int aff=preparedStatement.executeUpdate();
                    System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++");
                    System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++");
                    System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++");
                    System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++");
                    System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++");
                    System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++");
                    System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++");
                    System.out.println(aff+" Rows Affected");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        CourseDao cd=new CourseDao();
         cd.resetCourseStatus(courseName);
        return ResponseData.success();
    }


    @ResponseBody
    @RequestMapping("barchart")
    public ResponseData barchart(){
        BarChartRsp barChartRsp = this.eduCourseSelectionInfoService.barChart();
        return ResponseData.success(barChartRsp);
    }


    @ResponseBody
    @RequestMapping("linechart")
    public ResponseData line(){
        return ResponseData.success(this.eduCourseSelectionInfoService.lineChart());
    }
}


